package com.jeewms.www.tms;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.provider.MediaStore;
import android.util.Log;

import com.android.volley.VolleyError;
import com.jeewms.www.tms.bean.vm.wmapiUtil.ResultDO;
import com.jeewms.www.tms.ui.acitivity.Kcqr_Activity;
import com.jeewms.www.tms.ui.acitivity.Matnr_Activity;
import com.jeewms.www.tms.ui.acitivity.Resum_Activity;
import com.jeewms.www.tms.util.GsonUtils;
import com.jeewms.www.tms.util.Logutil;
import com.jeewms.www.tms.util.z_toast;
import com.jeewms.www.tms.volley.HTTPUtils;
import com.jeewms.www.tms.volley.VolleyListener;

import java.util.UUID;

import okhttp3.HttpUrl;

public class AutoReceiver extends BroadcastReceiver {
    // private static final int NOTIFICATION_FLAG = 1;
    public Context context1;
    private int NOTIFICATION_FLAG1 = UUID.randomUUID().hashCode();
    @Override
    public void onReceive(Context context, Intent intent) {
        Logutil.print("触发services");
        final Context context2 = context;
        if (intent.getAction().equals("VIDEO_TIMER")) {
//            PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
//                    new Intent(context, Matnr_Activity.class), 0);
            // 通过Notification.Builder来创建通知，注意API Level
            // API16之后才支持
            //调取WMS JEECG 后台数据出错的数据.
            String url = "http://192.168.200.100/zzwms/rest/wmbaseapi/getmessage?username=admin";
            // HTTPUtils.get();
            Logutil.print("触发services2");
            HTTPUtils.get(getActivity(), url, new VolleyListener() {


                @Override
                public void onResponse(String response) {
                    Logutil.print("触发services3");
                    ResultDO vm = GsonUtils.parseJSON(response, ResultDO.class);
                    String S = null;
                //    Logutil.print(vm.getObj().toString());
                    if (vm.getObj()!= null) {
                        for (int i = 0; i < vm.getObj().size(); i++) {
                            S = S + vm.getObj().get(i).wmX3.toString();
                        }
                        Logutil.print(S);
                        PendingIntent pendingIntent = PendingIntent.getActivity(context2, 0,
                                new Intent(context2, Matnr_Activity.class), 0);
                        //  audioManager.adjustStreamVolume(int streamType, int direction, int flags);
                        AudioManager am = (AudioManager) context2.getSystemService(Service.AUDIO_SERVICE);
                        am.adjustStreamVolume(AudioManager.STREAM_RING, AudioManager.ADJUST_RAISE, AudioManager.FLAG_VIBRATE);
                        Notification notify = new Notification.Builder(context2)
                                .setSmallIcon(R.drawable.ic_sy_error)
                                .setTicker("操作失败:" + "请注意查收")
                                .setContentTitle(S)
                                .setContentText("单号+物料号+数量")
                                .setDefaults(Notification.DEFAULT_ALL)
                                .setContentIntent(pendingIntent).setNumber(1).build(); // 需要注意build()是在API
                        // level16及之后增加的，API11可以使用getNotificatin()来替代
                        Intent intent1 = new Intent(context2, Kcqr_Activity.class);
                        PendingIntent pendingIntent1 = PendingIntent.getActivity(context2, 0, intent1, PendingIntent.FLAG_CANCEL_CURRENT);
                        // Builder.setContentIntent(pendingIntent);
                        //   PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent1,);
                        notify.flags |= Notification.FLAG_AUTO_CANCEL; // FLAG_AUTO_CANCEL表明当通知被用户点击时，通知将被清除。
                        // 在Android进行通知处理，首先需要重系统哪里获得通知管理器NotificationManager，它是一个系统Service。
                        NotificationManager manager = (NotificationManager) context2
                                .getSystemService(Context.NOTIFICATION_SERVICE);
                        manager.notify(NOTIFICATION_FLAG1, notify);// 步骤4：通过通知管理器来发起通知。如果id不同，则每click，在status哪里增加一个提示

                    }
                }

                @Override
                public void onErrorResponse(VolleyError error) {
                    Logutil.print("连接错误");
                }

            });


//            Logutil.print("第一次调用服务!");
//            //  audioManager.adjustStreamVolume(int streamType, int direction, int flags);
//            AudioManager am = (AudioManager) context.getSystemService(Service.AUDIO_SERVICE);
//            am.adjustStreamVolume(AudioManager.STREAM_RING, AudioManager.ADJUST_RAISE, AudioManager.FLAG_VIBRATE);
//            Notification notify = new Notification.Builder(context)
//                    .setSmallIcon(R.drawable.ic_sy_error)
//                    .setTicker("操作失败:" + "请注意查收")
//                    .setContentTitle("执行失败提醒")
//                    .setContentText("单号+物料号+数量")
//                    .setDefaults(Notification.DEFAULT_ALL)
//                    .setContentIntent(pendingIntent).setNumber(1).build(); // 需要注意build()是在API
//            // level16及之后增加的，API11可以使用getNotificatin()来替代
//            Intent intent1 = new Intent(context, Kcqr_Activity.class);
//            PendingIntent pendingIntent1 = PendingIntent.getActivity(context, 0, intent1, PendingIntent.FLAG_CANCEL_CURRENT);
//            // Builder.setContentIntent(pendingIntent);
//            //   PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent1,);
//            notify.flags |= Notification.FLAG_AUTO_CANCEL; // FLAG_AUTO_CANCEL表明当通知被用户点击时，通知将被清除。
//            // 在Android进行通知处理，首先需要重系统哪里获得通知管理器NotificationManager，它是一个系统Service。
//            NotificationManager manager = (NotificationManager) context
//                    .getSystemService(Context.NOTIFICATION_SERVICE);
//            manager.notify(NOTIFICATION_FLAG1, notify);// 步骤4：通过通知管理器来发起通知。如果id不同，则每click，在status哪里增加一个提示

        }
    }



    public Context getActivity() {
        return context1;
    }
}